package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * controller拦截器
 * 统一处理异常信息
 */
@Slf4j
@RestControllerAdvice
public class ConstrollerAspect {

    /**
     * 自定义异常处理
     *
     * @param apiException
     * @return
     */
    @ExceptionHandler(ApiException.class)
    public ResponseWrapper handleApiException(ApiException apiException) {

        log.warn("apiException,code [{}], mesage [{}]", apiException.getCode(), apiException.getMessage());
        return ResponseWrapper.failed(apiException.getCode(), apiException.getMessage());
    }

    /**
     * 参数不合法异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseWrapper handleIllegalArgumentException(IllegalArgumentException e) {
        log.warn("IllegalArgumentException: {}", e.getMessage());

        return ResponseWrapper.failed(ErrorCodeEnum.SW100);
    }

    /**
     * 未知异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseWrapper handleException(Exception e) {
        log.error("occur unknown exception statck", e);
        return ResponseWrapper.failed(ErrorCodeEnum.FL500);
    }
}
